package com.Drools.bean;

import org.drools.compiler.kie.builder.impl.KieBuilderImpl;
import org.kie.api.KieBase;
import org.kie.api.KieServices;
import org.kie.api.builder.*;
import org.kie.api.definition.KiePackage;
import org.kie.api.definition.rule.Rule;
import org.kie.api.io.KieResources;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

public class KieAll {
    public static void main(String[] args) {
        KieAll k = new KieAll();
        Scanner s = new Scanner(System.in);
        while(true){

            KieSession kieSession = k.getKieBase(s.nextLine(),s.nextLine(),s.nextLine(),s.nextLine()).newKieSession();
            int i = kieSession.fireAllRules();
            System.out.println(i);
            kieSession.dispose();//关闭规则
        }
    }
    private KieServices kieServices = KieServices.Factory.get();//这个是全局常量，唯一

    private KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
    private KieBuilder kieBuilder ;

    static int a = 1;


    public void write(byte[] bytes){
//        kieFileSystem.write("src/main/resources/rules/"+ a++ +".ruler.drl",bytes);
        kieFileSystem.write("src/main/resources/rules/"+ bytes +".ruler.drl",
                ("rule \"test"+ a++ +"\" when eval(true); then System.out.println(\"测试----组--1--eee---\"); end").getBytes());


        kieServices.newKieBuilder(kieFileSystem).buildAll();

    }

    public KieBase getKieBase(String pg,String test,String fi,String ContainerId){//pg 包名   test 规则名    fi 文件名

//        kieServices.

//        kieFileSystem.write("src/main/resources/rules/"+ a++ +".ruler.drl",bytes);
        System.out.println("------kieFileSystem--------------------");
        kieFileSystem.write("src/main/resources/rules/"+ fi +".ruler.drl", //名字重复直接覆盖
                //        rule 名字重复,添加失败
                ("package sss"+ pg +";rule \"test"+ test +"\" when eval(true); then System.out.println(\""+ LocalDateTime.now() +"\"); end").getBytes());
//        kieFileSystem.delete("src/main/resources/rules/2.ruler.drl");//可删除文件规则指定

        try {
//            System.out.println(new String(kieFileSystem.read("src/main/resources/rules/1.ruler.drl")));//查到指定 语句
        }catch (Exception e){
            System.out.println("不存在1.ruler.drl");
            throw new RuntimeException("");
        }

        System.out.println("------KieBuilder--------------------");
        kieBuilder = kieServices.newKieBuilder(kieFileSystem);

        kieBuilder.buildAll();//将 kieBuilder 里的规则 构建

        System.out.println("------KieBuilder.getResults--------------------");
        Results results = kieBuilder.getResults();//不认识
        System.out.println(results);
        System.out.println("------KieModule--------------------");
        KieModule kieModule = kieBuilder.getKieModule();
        ReleaseId releaseId = kieModule.getReleaseId();
        System.out.println(releaseId);

//        KieContainer kieContainer = kieServices.newKieContainer(releaseId);
//        获取配置文件的kbase();

        System.out.println("------KieContainer--------------------");
        KieContainer kieContainer = kieServices.newKieContainer(kieServices.getRepository().getDefaultReleaseId());
//        KieContainer kieContainer = kieServices.getKieClasspathContainer(ContainerId);

        System.out.println("------getKieBaseNames--------------------");
        System.out.println(kieContainer.getKieBaseNames());//输出  每个KieBase的名字

//        System.out.println("------kieScanner-------------");
//        KieScanner kieScanner = kieServices.newKieScanner(kieContainer);//maven 配才行

        System.out.println("------KieBase--------------------");
//        KieBase kieBase = kieContainer.getKieBase();
        KieBase kieBase = kieContainer.getKieBase("defaultKieBase");//获取指定 名字的 KieBase  指kmodule.xml kbase 的name
        System.out.println("------KiePackage--------------------");

        Collection<KiePackage> kiePackages = kieBase.getKiePackages();//获取有多少  package 的包名
        int i = 0;
        for (KiePackage s:kiePackages){//便利 每个包
            System.out.println("------getRules:"+ i++ +"-------------");
            System.out.println("包名：" + s.getName());
            for (Rule r:s.getRules()){//便利每个包下的 规则
                System.out.println("规则名：" + r.getName());
            }
        }
        System.out.println("------kieBase-------------");
        System.out.println(kieBase.getRule("sss1", "test1"));


        return kieBase;
    }

    private void 获取配置文件的kbase() {
        System.out.println("------getKieClasspathContainer--------------------");
        KieContainer kieClasspathContainer = kieServices.getKieClasspathContainer();//找kmodule.xml
        System.out.println(kieServices.getKieClasspathContainer().getKieBaseNames());//获取所有 KieBase的名字
        KieBase myKbase3 = kieClasspathContainer.getKieBase("myKbase3");
    }


    public KieBase getKieBase(byte[] bytes){
        write(bytes);
        KieContainer kieContainer = kieServices.newKieContainer(kieServices.getRepository().getDefaultReleaseId());
        KieBase kieBase = kieContainer.getKieBase();

        return kieBase;
    }
}
